#include"TcpServer.hpp"
#include<iostream>
#include<string>
#include<memory>
#include"Socket.hpp"
#include"Accepter.hpp"
using namespace NetWorkModule;

static const int gbacklog = 32;
void Usage(std::string process)
{
    std::cerr<<"Usage: "<<process<<" port"<<std::endl;
}


void Test(Connection *conn)
{
    std::cout << "get event, sockfd is : " << conn->SockFd() << std::endl;
}
int main(int argc,char *argv[])
{
    if(argc!=2)
    {
        Usage(argv[0]);
        return 1;
    }
    int port=std::stoi(argv[1]);
    //创建listensocket
    std::unique_ptr<Socket> listensocket=std::make_unique<TcpSocket>();
    listensocket->BulidListenSocketMethod(port,backlog);

    //创建TCPserver
    std::unique_ptr<TcpServer> svr=std::make_unique<TcpServer>();
    std::unique_ptr<Accepter> accepter=std::make_unique<Accepter>();
    auto listen_recver=std::bind(&Accepter::AccepterConnection,accepter.get(),std::placeholders::_1);
    Connection *conn=ConnectionFactory::BuildListenConnectiom(listensocket->GetSockFd(),
    listen_recver,EPOLLIN|EPOLLET,svr.get());

    svr->AddConnection(conn);
    svr->Dispatcher();
    return 0;
}